import {fill_cell, get_cell_value, set_cell_color, set_cells} from "@/components/js/systemdemo";
import Enum from "@/components/js/enum";
import lodash from "lodash";

let app = wps.EtApplication();

export function 高压缸效率(row) {
    /**
     * 在“汽机"页面中写入高压缸计算结果区域。高压缸效率只与高压缸进出口蒸汽参数有关。
     * 在某些机组中，汽轮机高压缸前轴封一漏会经高压缸内外缸夹层漏汽汇入高压缸排汽，性能试验中测得的高压缸排汽测点在漏汽汇入点之后，实际上性能试验
     * 测得的高压缸效率是包含了夹层漏汽之后的高压缸效率，这个效率比真实的高压缸效率低。但是，因为这股漏汽是位于汽轮机高压缸内部的，一般将其作为
     * 高压缸固有属性来考虑，因此，厂家给出的高压缸效率保证值就是性能试验测点直接计算得到的高压缸效率。而真是的高压缸效率需要考虑夹层漏汽流量、
     * 漏汽焓值等更多参数的影响。
     * @type {string[][]}
     */
    wps.PluginStorage.setItem("高压缸效率_info", JSON.stringify({"row": row}))
    let data = [
        ["高压缸", "主蒸汽压力", "MPa"],
        ["", "主蒸汽温度", "℃"],
        ["", "主蒸汽焓值", "kJ/kg"],
        ["", "主蒸汽熵值", "kJ/(kgK)"],
        ["", "主蒸汽流量", "t/h"],
        ["", "高排压力", "MPa"],
        ["", "高排温度", "℃"],
        ["", "高排焓值", "kJ/kg"],
        ["", "高排流量", "t/h"],
        ["", "冷再流量", "t/h"],
        ["", "一段抽汽流量", "t/h"],
        ["", "二段抽汽流量", "t/h"],
        ["", "高压缸效率", "%"],
    ];
    set_cells(row + 2, 1, data);
    set_cell_color(row + 2, 1, 4); // 设置第一个单元格背景色
    app.Goto(app.ActiveSheet.Cells.Item(row + 2, 1), true); //滚动窗口到更改内容
    let cols = wps.PluginStorage.getItem("sheet_columns")
    let sp = Enum.StandardPoint;
    let leakage = sp.leakage.obj;
    let source = leakage.get_source("高排"); // 有漏汽至高排，则默认漏汽至高排测点之前，即实际测量的高排参数为漏汽和高排混合后的参数
    let leakage_F = `{leakage.${source}流量}`
    for (let i = 4; i <= cols; i++) {
        // 工况列从第4(D)列开始
        fill_cell(row + 2, i, "para", "main_steam.P");
        fill_cell(row + 3, i, "para", "main_steam.T");
        fill_cell(row + 4, i, "formula", "{main_steam.H}=H_PT({row-2}, {row-1})");
        fill_cell(row + 5, i, "formula", "{main_steam.S}=S_PT({row-3}, {row-2})");
        fill_cell(row + 6, i, "para", "main_steam.F", "watch")

        fill_cell(row + 7, i, "para", "{outlet_high.P}"); // 默认忽略夹层漏汽
        fill_cell(row + 8, i, "para", "{outlet_high.T}");
        sp.insert_para_link("outlet_high.P", `汽机!{Y}${row + 7}`)
        sp.insert_para_link("outlet_high.T", `汽机!{Y}${row + 8}`)
        fill_cell(row + 9, i, "formula", `{outlet_high.H}=H_PT({row-2},{row-1})`);
        fill_cell(row + 10, i, "formula", `{outlet_high.F}={row-4}-{row+2}-{leakage.高压前轴封漏汽量}-{leakage.高压后轴封漏汽量}-{leakage.高压门杆漏汽量}+${leakage_F}`)
        let 高排抽汽 = JSON.parse(wps.PluginStorage.getItem("高排抽汽"))
        if (lodash.isEqual(高排抽汽, [2])) {
            fill_cell(row + 11, i, "formula", `{cold_reheat_steam.F}={row-1}-{row+2}`);
        } else {
            fill_cell(row + 11, i, "formula", `{cold_reheat_steam.F}={row-1}`);
        }
        fill_cell(row + 12, i, "para", "extract_1.F", "watch")
        fill_cell(row + 13, i, "para", "extract_2.F", "watch")
        fill_cell(row + 14, i, "formula", "{turbine_high.eta}=({row-10}-{row-5})/({row-10}-H_PS({row-7},{row-9}))*100");
    }

    let current_selected_value = "忽略夹层漏汽"
    // 添加漏气至高排测点位置下拉选项
    if (source) {
        let cell = get_cell_value(row + 14, 1, undefined, true)
        cell.Validation.Add(3, null, null, "漏汽至高排测点之前,漏汽至高排测点之后,忽略夹层漏汽", 1)
        cell.Value2 = current_selected_value
        cell.Validation.InputMessage = JSON.stringify({"value": current_selected_value, "id": 1}) // 当下拉列表中的值改变时，这里的value记录改变前的值
        cell.Validation.ShowInput = false; // 隐藏掉InputMessage的显示，因为Formula2是只读的，这里使用InputMessage保存单元格的额外信息，不像用户显示该信息
        set_cell_color(row + 14, 1, 6)
    }
    wps.PluginStorage.setItem("leakage_to_outlet_high", JSON.stringify({
        "F": leakage_F,
        "type": current_selected_value
    }))
}

export function modify高压缸效率(cell, old_value) {
    /**
     * 更改二抽中关于漏汽的计算细节，只有存在漏至二抽的蒸汽时，才会出现执行到该函数的下拉列表，也才会执行到该函数。
     *
     * @type {string}
     */
    let new_value = cell.Value2.toString();
    let info = JSON.parse(wps.PluginStorage.getItem("高压缸效率_info"))
    let row = info.row;
    let sp = Enum.StandardPoint;
    let leakage = sp.leakage.obj;
    let source = leakage.get_source("高排"); // 漏至高排的抽汽的来源，一般为高压前轴封一漏
    let leakage_F = `{leakage.${source}流量}`;
    let cols = wps.PluginStorage.getItem("sheet_columns")
    let 高排抽汽 = JSON.parse(wps.PluginStorage.getItem("高排抽汽"))
    if (new_value === "漏汽至高排测点之前") {
        wps.PluginStorage.setItem("leakage_to_outlet_high", JSON.stringify({"F": leakage_F, "type": "漏汽至高排测点之前"}))
        for (let i = 4; i <= cols; i++) {
            // 工况列从第4(D)列开始
            sp.set_para_link("outlet_high.P_mixed", sp.get_para_link("outlet_high.P")) // 试验中的高排参数实际为混合后参数
            sp.set_para_link("outlet_high.T_mixed", sp.get_para_link("outlet_high.T")) // 试验中的高排参数实际为混合后参数
            fill_cell(row + 7, i, "para", "{outlet_high.P}"); // 蒸汽混合前后压力不变，因此高排压力=混合后压力
            sp.delete_para("outlet_high.T")  // 混合前后温度会发生变化，因此高排温度不能直接使用测量的数值
            fill_cell(row + 8, i, "formula", "{outlet_high.T}=T_PH({row-1},{row+1})");
            fill_cell(row + 9, i, "formula", `{outlet_high.H}=(({row+1}+${leakage_F})*H_PT({outlet_high.P_mixed},{outlet_high.T_mixed})-${leakage_F}*{main_steam.H})/{row+1}`);
            fill_cell(row + 10, i, "formula", "{outlet_high.F}={row-4}-{row+2}-{leakage.高压前轴封漏汽量}-{leakage.高压后轴封漏汽量}-{leakage.高压门杆漏汽量}")

            if (lodash.isEqual(高排抽汽, [2])) {
                fill_cell(row + 11, i, "formula", `{cold_reheat_steam.F}={row-1}-{row+2}+${leakage_F}`)
            } else {
                fill_cell(row + 11, i, "formula", `{cold_reheat_steam.F}={row-1}+${leakage_F}`)
            }
        }

    } else if (new_value === "漏汽至高排测点之后") {
        // 一般不会出现这种情况，参加下一个else模块中的注释。如果出现这种情况，则
        // 测量点的二抽压力、温度实际为二抽与漏汽混合后的压力和温度
        if (old_value === "漏汽至高排测点之前") {
            sp.set_para_link("outlet_high.T", sp.get_para_link("outlet_high.T_mixed")) // 试验中的高排参数实际为混合后参数
        }
        wps.PluginStorage.setItem("leakage_to_outlet_high", JSON.stringify({"F": leakage_F, "type": "漏汽至高排测点之后"}))
        for (let i = 4; i <= cols; i++) {
            // 工况列从第4(D)列开始
            fill_cell(row + 7, i, "para", "{outlet_high.P}"); // 蒸汽混合前后压力不变，因此高排压力=混合后压力
            fill_cell(row + 8, i, "para", "{outlet_high.T}");
            fill_cell(row + 9, i, "formula", `{outlet_high.H}=H_PT({row-2},{row-1})`);
            fill_cell(row + 10, i, "formula", "{outlet_high.F}={row-4}-{row+2}-{leakage.高压前轴封漏汽量}-{leakage.高压后轴封漏汽量}-{leakage.高压门杆漏汽量}")
            if (lodash.isEqual(高排抽汽, [2])) {
                fill_cell(row + 11, i, "formula", `{cold_reheat_steam.F}={row-1}-{row+2}+${leakage_F}`)
            } else {
                fill_cell(row + 11, i, "formula", `{cold_reheat_steam.F}={row-1}+${leakage_F}`)
            }
        }
    } else if (new_value === "忽略夹层漏汽") {
        // 一般来说，漏至二抽的漏汽来自高压缸前轴封一漏或夹层漏汽，但这部分漏汽从高压缸前轴封经夹层漏至高压缸排汽中，汽缸外部看不到测点。
        // 通常情况下，汽机厂家给出的高压缸效率是考虑了这部分漏汽之后的效率，性能试验时可以认为这部分漏汽属于汽缸本身的特性，因此可以忽略
        // 该股漏汽。直接用主汽和漏汽后的高排测点计算高压缸效率作为试验值与性能保证值进行对比。
        if (old_value === "漏汽至高排测点之前") {
            sp.set_para_link("outlet_high.T", sp.get_para_link("outlet_high.T_mixed")) // 试验中的高排参数实际为混合后参数
        }
        wps.PluginStorage.setItem("leakage_to_outlet_high", JSON.stringify({"F": leakage_F, "type": "忽略夹层漏汽"}))
        for (let i = 4; i <= cols; i++) {
            // 工况列从第4(D)列开始
            fill_cell(row + 2, i, "para", "main_steam.P");
            fill_cell(row + 3, i, "para", "main_steam.T");
            fill_cell(row + 4, i, "formula", "{main_steam.H}=H_PT({row-2}, {row-1})");
            fill_cell(row + 5, i, "formula", "{main_steam.S}=S_PT({row-3}, {row-2})");
            fill_cell(row + 6, i, "para", "main_steam.F", "watch")

            fill_cell(row + 7, i, "para", "{outlet_high.P}"); // 蒸汽混合前后压力不变，因此高排压力=混合后压力
            fill_cell(row + 8, i, "para", "{outlet_high.T}");
            fill_cell(row + 9, i, "formula", `{outlet_high.H}=H_PT({row-2},{row-1})`);
            fill_cell(row + 10, i, "formula", `{outlet_high.F}={row-4}-{row+2}-{leakage.高压前轴封漏汽量}-{leakage.高压后轴封漏汽量}-{leakage.高压门杆漏汽量}+${leakage_F}`)
            if (lodash.isEqual(高排抽汽, [2])) {
                fill_cell(row + 11, i, "formula", `{cold_reheat_steam.F}={row-1}-{row+2}`)
            } else {
                fill_cell(row + 11, i, "formula", `{cold_reheat_steam.F}={row-1}`)
            }
        }
    }
}